cmake_minimum_required(VERSION 3.1)

project(CG)

set(CMAKE_CXX_STANDARD 17) # this does nothing for MSVC, use target_compile_options below
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

# 处理msvc乱码
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/source-charset:utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/execution-charset:gbk>")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/execution-charset:gbk>")

if(NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

if(WIN32)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
endif(WIN32)

link_directories(${CMAKE_SOURCE_DIR}/lib)

find_package(Freeglut REQUIRED)
message(STATUS "FREEGLUT included at ${FREEGLUT_INCLUDE_DIR}")
include_directories(${CMAKE_SOURCE_DIR}/include)
set(LIBS ${Freeglut_LIBRARIES})

# ###############添加一个源程序####################
# 设置章节名，对应src目录下的每个目录（这些目录需要用户自己创建）
set(CHAPTERS
    Chapter2.GrahpicSystem
    Chapter3.Rasterization
    Chapter4.GrahpicTransform
    Chapter5.3DObservation
    Chapter6.3DConfiguration
    experiment
    test # 1. 如：添加一个test章节
)

# 设置每章下的工程
set(
    Chapter2.GrahpicSystem
    2.4.5.OpenGLSample
)

set(
    Chapter3.Rasterization
    Chapter3.1.DrawLineDDA
)
set(
    Chapter4.GrahpicTransform
    4.4.TransformSample
    4.4.2.RectangleTransform
    4.4.3.RotateCube
)
set(
    Chapter5.3DObservation
    5.5.1.RedBlueTriangles
    5.5.2.PerspectCube
)
set(
    Chapter6.3DConfiguration
    6.3.4.SimpleEntityConfiguration
    6.4.6.1.CurveGeneration
)
set(
    experiment
    experiment1.drawTriangle
    experiment1.1.centerWindow
    experiment2.mouseDrawLine
    experiment3.rotateCube
    experiment4.bezier
)

# 2. 添加章节下的工程，工程名为：first_test
set(
    test # 3. 在src目录下创建test文件夹，然后在test文件夹创建first_test文件夹
    first_test # 4. 最后在first_test中新建main.cpp即可
    
)


function(create_project_from_sources chapter demo)
    file(GLOB SOURCE
        "src/${chapter}/${demo}/*.h"
        "src/${chapter}/${demo}/*.cpp"
        "src/${chapter}/${demo}/*.vs"
        "src/${chapter}/${demo}/*.fs"
        "src/${chapter}/${demo}/*.gs"
        "src/${chapter}/${demo}/*.cs"
    )

    if(demo STREQUAL "")
        SET(replaced "")
        string(REPLACE "/" "_" replaced ${chapter})
        set(NAME ${replaced})
    else()
        set(NAME "${chapter}__${demo}")
    endif()

    add_executable(${NAME} ${SOURCE})
    target_link_libraries(${NAME} ${LIBS})

    if(MSVC)
        target_compile_options(${NAME} PRIVATE /std:c++17 /MP)
        #starget_link_options(${NAME} PUBLIC /ignore:4099)
    endif(MSVC)

    if(WIN32)
        set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${chapter}")
        set_target_properties(${NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${chapter}/Debug")
    elseif(UNIX AND NOT APPLE)
        set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${chapter}")
    elseif(APPLE)
        set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${chapter}")
        set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/bin/${chapter}")
        set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/bin/${chapter}")
    endif(WIN32)

    # copy shader files to build directory
    file(GLOB SHADERS
        "src/${chapter}/${demo}/*.vs"

        # "src/${chapter}/${demo}/*.frag"
        "src/${chapter}/${demo}/*.fs"
        "src/${chapter}/${demo}/*.gs"
        "src/${chapter}/${demo}/*.cs"
    )

    # copy dlls
    file(GLOB DLLS "dlls/*.dll")
    add_custom_command(TARGET ${NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${DLLS} $<TARGET_FILE_DIR:${NAME}>)
    foreach(SHADER ${SHADERS})
        if(WIN32)
            # configure_file(${SHADER} "test")
            add_custom_command(TARGET ${NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SHADER} $<TARGET_FILE_DIR:${NAME}>)
            add_custom_command(TARGET ${NAME} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${DLLS} $<TARGET_FILE_DIR:${NAME}>)
        elseif(UNIX AND NOT APPLE)
            file(COPY ${SHADER} DESTINATION ${CMAKE_SOURCE_DIR}/bin/${chapter})
        elseif(APPLE)
            # create symbolic link for *.vs *.fs *.gs
            get_filename_component(SHADERNAME ${SHADER} NAME)
            makeLink(${SHADER} ${CMAKE_SOURCE_DIR}/bin/${chapter}/${SHADERNAME} ${NAME})
        endif(WIN32)
    endforeach(SHADER)

    # if compiling for visual studio, also use configure file for each project (specifically to set up working directory)
    if(MSVC)
        configure_file(${CMAKE_SOURCE_DIR}/configuration/visualstudio.vcxproj.user.in ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.vcxproj.user @ONLY)
    endif(MSVC)
endfunction()

# then create a project file per tutorial
foreach(CHAPTER ${CHAPTERS})
    foreach(DEMO ${${CHAPTER}})
        create_project_from_sources(${CHAPTER} ${DEMO})
    endforeach(DEMO)
endforeach(CHAPTER)

foreach(GUEST_ARTICLE ${GUEST_ARTICLES})
    create_project_from_sources(${GUEST_ARTICLE} "")
endforeach(GUEST_ARTICLE)